﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Microsoft.Windows.Controls.Ribbon;
using CommonSense.UI.Shell.Services;
using CommonSense.UI.Shell.Views;
using CommonSense.UI.Shell.ViewModels;
using CommonSense.UI.Shell.Interfaces;
using System.Windows.Media.Imaging;
using System.Windows.Interop;

namespace CommonSense.UI.Shell
{
    public class ShellApplication : Application
    {
        private RootViewModel rootViewModel;
        private IModuleService moduleService;
        private IWorkspaceService workspaceService;
        private IViewModelFactory viewModelFactory;

        public System.Drawing.Icon Icon
        {
            get;
            set;
        }
     
        public ShellApplication(IDIContainer diContainer)
        {
            this.moduleService = new ModuleService(diContainer);
            this.viewModelFactory = new ViewModelFactory(this.moduleService);
        }

        public void RegisterAssembly(Assembly assembly)
        {
            this.moduleService.RegisterAssembly(assembly);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            this.SetupModules();

            this.MainWindow = new RibbonWindow();
            this.MainWindow.Background = Brushes.Orange;
            this.MainWindow.Width = 1024;
            this.MainWindow.Height = 768;
            this.MainWindow.WindowState = WindowState.Maximized;
            this.MainWindow.Content = this.rootViewModel.View;
            this.MainWindow.Icon = Imaging.CreateBitmapSourceFromHIcon(this.Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            this.MainWindow.Show();
            base.OnStartup(e);
        }

        protected virtual void SetupModules()
        {
            IEnumerable<IModule> modules = this.moduleService.GetModules();
            
            foreach (IModule module in modules)
            {
                ResourceDictionary resources = module.GetModuleResouces();
                if (resources != null)
                {
                    this.Resources.MergedDictionaries.Add(resources);
                }
            }

            this.rootViewModel = this.viewModelFactory.Create<RootViewModel>();
            this.workspaceService = new WorkspaceService(this.moduleService, this.rootViewModel);
            this.moduleService.DIContainer.Configure();
            RootView rootView = (RootView)this.rootViewModel.View;
           
            List<RibbonTab> ribbonTabs = new List<RibbonTab>();
            foreach (IModule module in modules)
            {
                foreach(RibbonTab ribbonTab in module.GetRibbonTabs())
                {
                    string viewModelTypeName = (string)ribbonTab.DataContext;
                    Type viewModelType = moduleService.GetTypeByName(viewModelTypeName);
                    ribbonTab.DataContext = moduleService.CreateInstance(viewModelType);
                    rootView.ribbon.Items.Add(ribbonTab);
                }
            }
        }
    }
}
